package logic

import (
	"context"
	"fmt"
	"usersrv/model/mysql"

	"usersrv/internal/svc"
	"usersrv/user"

	"github.com/zeromicro/go-zero/core/logx"
)

type LonginLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewLonginLogic(ctx context.Context, svcCtx *svc.ServiceContext) *LonginLogic {
	return &LonginLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *LonginLogic) Longin(in *user.LonginRequest) (*user.LonginResponse, error) {
	// todo: add your logic here and delete this line
	var u mysql.User
	err := u.GetUserInfo(in.Username)
	if err != nil {
		return nil, fmt.Errorf("登录失败")
	}
	if u.ID == 0 {
		var users = mysql.User{
			Username: in.Username,
			Password: in.Password,
		}
		users.Create()
	}
	if u.Password != in.Password {
		return nil, fmt.Errorf("密码不正确")
	}

	return &user.LonginResponse{Id: int64(u.ID)}, nil
}
